package com.sgb.kjwl.view.ui.activity.self.logistics

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.util.TypedValue
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.GridLayoutManager
import com.fondesa.recyclerviewdivider.dividerBuilder
import com.sgb.kjwl.AppActivity
import com.sgb.kjwl.R
import com.sgb.kjwl.databinding.ActivitySeeAttachmentBinding
import com.sgb.kjwl.utils.DisplayUtil
import com.sgb.kjwl.view.ui.activity.PhotoViewActivity
import com.sgb.kjwl.view.ui.adapter.transport.CertificateImageAdapter

/**
 *   Describe:  查看附件（报价）
 *   Author:  yangLei
 *   Date:    2021/7/19
 */
class SeeAttachmentActivity : AppActivity() {
    private lateinit var mBinging: ActivitySeeAttachmentBinding
    private lateinit var mAdapter: CertificateImageAdapter
    private lateinit var mList: ArrayList<String>

    companion object {
        fun start(context: Context, images: ArrayList<String>?) {
            val intent = Intent(context, SeeAttachmentActivity::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
            intent.putStringArrayListExtra("strings", images)
            context.startActivity(intent)
        }
    }

    override fun initView() {
        var list = intent.getStringArrayListExtra("strings")
        if (list == null || list.isEmpty()) {
            finish()
        } else {
            mList = ArrayList()
            mList.addAll(list)
        }
        mBinging = DataBindingUtil.setContentView(this, R.layout.activity_see_attachment)
        mBinging.topBar.titleView.text = "查看附件"
        mBinging.topBar.ivBack.setOnClickListener { finish() }
        mBinging.rvPhotos.layoutManager = GridLayoutManager(this, 4)
        this.dividerBuilder()
            .color(Color.TRANSPARENT)
            .size(10, TypedValue.COMPLEX_UNIT_DIP)
            .build()
            .addTo(mBinging.rvPhotos)
        mAdapter = CertificateImageAdapter(mList, DisplayUtil.dip2px(this, 2f))
        mAdapter.bindToRecyclerView(mBinging.rvPhotos)
        mAdapter.setOnItemClickListener { _, _, position ->
            viewPhotoDetails(position)
        }
    }

    /**
     * 查看照片详情
     */
    private fun viewPhotoDetails(position: Int) {
        var intentSkip = Intent()
        intentSkip.setClass(this, PhotoViewActivity::class.java)
        intentSkip.putExtra(PhotoViewActivity.KEY_PHOTO_URLS, mList)
        intentSkip.putExtra(PhotoViewActivity.KEY_PHOTO_POSITION, position)
        startActivity(intentSkip)
    }
}